
var gridFeild = 'id';//需从grid-select带入显示的字段值
var gridTitle = '选择数据';//弹出框的标题名称
var formLable = 'lableName';//表单的显示字段
/*
 * custom_element custom类型必须设置的类型.可选gridSelectElem,treeSelectElem,也可以自定义函数
 * custom_value custom类型必须设置的类型.可选gridSelectElemValue,treeSelectElemValue,也可以自定义函数
 * gridId 弹出列表的id
 * gridUrl 弹出列表的跳转地址
 * rowFeild 需从grid-select带入显示的字段值
 * lableName 显示在表单的字段名称
 * title 弹框标题
 * callBack 回调函数
 * */
/**
 * @description 弹出datagrid的列表，选择数据，jqgrid的custom类型必须设置.<br>
 * @param value 当前值.<br>
 * @param options 配置的参数.<br>
 * @date 2018/1/14.<br>
 * @author zcg.<br>
 */
function gridSelectElem(value, options) {
	if(typeof (options) == "object" && Object.prototype.toString.call(options).toLowerCase() == "[object object]" && !options.length){
		
	}else{
		options = {};
	}
	if(!options.callBack){options.callBack = function(){}};
	if(!options.lableName){options.lableName = formLable};
	if(!options.title){options.title = gridTitle};
	if(!options.rowFeild){options.rowFeild = gridFeild};
	
	var callBack = options.callBack;
    var el=document.createElement("div");  
    el.setAttribute("class","input-group");
    el.setAttribute("onclick","gridSelect("+JSON.stringify(options)+","+callBack+")")
    
    var text = document.createElement("input");
    text.setAttribute("type","text");
    text.setAttribute("id",options.rowId+"_"+options.lableName);
    text.setAttribute("value",value);
    text.setAttribute("readonly","readonly")
    text.setAttribute("class","form-control");
    
    var span = document.createElement("span");
    span.setAttribute("class","input-group-btn");
    
    var btn = document.createElement("span");
    btn.setAttribute("type","button");
    btn.setAttribute("id","child");
    btn.setAttribute("class","btn btn-primary treebtn");
    
    var li = document.createElement("li");
    li.setAttribute("class","fa fa-search");
    btn.appendChild(li);
    span.appendChild(btn);
    
    el.appendChild(text);
    el.appendChild(span);  
    return el;
}

/**
 * @description 弹出datagrid的列表，选择数据.<br>
 * @param options 配置的参数.<br>
 * @param callBack 回调函数.<br>
 * @date 2018/1/14.<br>
 * @author zcg.<br>
 */
function gridSelect(options,callBack){
	var width = options.width = options.width?options.width:'60%';
	var height = options.height = options.height?options.height:'60%';
	var gridId = options.gridId;
	var title = options.title;
	var gridUrl = options.gridUrl;
	var lableName = options.lableName;
	var rowFeild = options.rowFeild;
	top.layer.open({
	    type: 2, 
	    area: [width, height],
	    title:title,
        maxmin: true, //开启最大化最小化按钮
	    content: gridUrl,
	    btn: ['确定', '关闭'],
	    yes: function(index, layero){ //或者使用btn1
	               	var grid=layero.find("iframe")[0].contentWindow.$("#"+gridId);//获取默认ID  
					var ids = [],lableNames=[];
					var rows = grid.jqGrid('getGridParam','selarrrow');
					var rowData= grid.jqGrid('getGridParam','selrow');
					var multiselect = grid.jqGrid('getGridParam','multiselect');
					if(!multiselect){
						if(rowData){
							rows[0]=rowData;
						}
					}
					var rowDatas=[];
					for ( var i = 0; i < rows.length; i++) {
						var rowId=rows[i];
		        		ids.push(rowId);
		        		var rowData =grid.jqGrid('getRowData',rowId);
		        		rowDatas.push(rowData);
		        		lableNames.push(rowData[rowFeild]);
					}
					var lableName = options.rowId+"_"+options.lableName;
					$('#'+lableName).val(lableNames.join(","));
					$("#"+lableName).focus();
					top.layer.close(index);
					callBack(rowDatas,options);
					
		   },
	      cancel: function(index){ //或者使用btn2
	           //按钮【按钮二】的回调
	      }
	}); 
}

/**
 * @description 回写函数,jqgrid的custom类型必须设置.<br>
 * @param operation 操作类型.<br>
 * @param callBack 回调函数.<br>
 * @date 2018/1/14.<br>
 * @author zcg.<br>
 */
function gridSelectElemValue(elem, operation, value) {
    if(operation === 'get') {
       return $(elem).find("input").val();
    } else if(operation === 'set') {
       $('input',elem).val(value);
    }
}